package com.codingInn.web.validator;

import com.codingInn.web.pojo.User;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Component
public class UserValidator implements Validator {
	@Override
	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	@Override
	public void validate(Object target, Errors errors) {
		User user = (User) target;

		if(user.getName().length() < 5){
			//第一个参数是错误字段，第二个参数是错误代码，第三个参数是错误参数，第四个参数是错误默认消息
			errors.rejectValue("name","valid.nameLen",new Object[]{"minLength",5},"用户名不能少于5位");
		}
	}
}
